<template>
  <div class="app-container">
    <div class="filter-container filter-container-left">
      <el-form class="form_search" :inline="true" label-width='90px'>
        <el-form-item label="用户名：">
          <el-input v-model="queryParam.username" placeholder="请输入用户名" clearable class="filter-item"></el-input>
        </el-form-item>
        <el-form-item label="账号：">
          <el-input v-model="queryParam.mobile" placeholder="请输入账号" clearable class="filter-item"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="filter-item search" type='primary' icon="el-icon-search" @click="handleSearch">查询</el-button>
          <el-button class="filter-item" icon="el-icon-refresh" @click="handleRefresh">重置</el-button>
          <el-button class="filter-item search" icon="el-icon-s-unfold" @click="handleExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="data"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <!-- <el-table-column prop="id" label="序号" width='80' align='center'/> -->
      <el-table-column label="用户名"  align='center'>
        <template slot-scope="item">
          <p v-if="item.row.user_info">{{item.row.user_info.username || item.row.user_info.mobile}}</p>
        </template>
      </el-table-column>
      <el-table-column prop="user_info.mobile" label="账号" align='center'/>
      <el-table-column label="余额" prop='balance' align='center'/>
      <el-table-column label="可提现" prop='balance' align='center'/>
      <el-table-column label="提现中" prop='withdrawal_in' align='center'/>
      <el-table-column label="冻结中" prop='frozen_balance' align='center'/>
      <el-table-column prop="count_income" label="总收入" align='center'/>
      <el-table-column prop="count_pay" label="总支出" align='center'/>
      <el-table-column label="操作" width='200' align='center'>
        <template slot-scope="item">
          <el-link type="primary" class="table_btn" @click="handleIncome(item.row)" :underline="false"><i class="iconfont icon-chakan"></i>查看</el-link>
          <el-link type="primary" class="table_btn" v-if='item.row.frozen_balance==0' @click="handleFrozen(item.row,1)" :underline="false"><i class="iconfont icon-bianji"></i>账户冻结</el-link>
          <el-link type="primary" class="table_btn" v-else @click="handleFrozen(item.row,2)" :underline="false"><i class="iconfont icon-bianji"></i>解冻</el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      class="pagination-container"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="paginate.current"
      :hide-on-single-page='isHideSingle'
      :page-sizes="paginate.sizes"
      :page-size="paginate.limit"
      :layout="paginate.layout"
      :total="paginate.total"/>
      <!-- 账户冻结 -->
    <el-dialog :visible.sync="formVisible2" @close="hide" width='654px' :show-close='false'>
      <div slot="title" class="dialog-title">{{status==1?'账户冻结':'账户解冻'}}</div>
      <el-card class="el-card-border">
        <el-form :ref="formName" :model="formFieldsData" label-width='90px' :label-position="labelPosition">
          <el-form-item label="用户：">
            <p v-if="details">{{details.user_info.username}}/{{details.user_info.mobile}}</p>
          </el-form-item>
          <el-form-item label="当前余额：">
            <p>￥{{details.balance}}</p>
          </el-form-item>
          <el-form-item label="冻结金额：" v-if="status==1">
            <el-input type="number" placeholder='请输入冻结金额' v-model="formFieldsData.frozen_balance" style="width: 60%;margin-right:10px"></el-input>
            <el-link :underline="false" @click="frozenAll">全部冻结</el-link>
          </el-form-item>
          <el-form-item label="冻结金额：" v-else>
            <span>￥{{details.frozen_balance}}</span>
          </el-form-item>
          <el-form-item label="解冻金额：" v-if="status==2">
            <el-input type="number" placeholder='请输入冻结金额' v-model="formFieldsData.balance" style="width: 60%;margin-right:10px"></el-input>
            <el-link :underline="false" @click="balanceAll">全部解冻</el-link>
          </el-form-item>
          <el-form-item label="备注：">
            <el-input type="textarea" v-model="formFieldsData.remake" style="width: 92%"></el-input>
          </el-form-item>
        </el-form>
        <div v-if="status==2">
          <p>历史冻结记录：</p>
          <el-table
            :data="frozenBalanceList"
            style="width: 100%;margin: 20px 0;"
            row-key="id"
            border
            default-expand-all
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          >
            <el-table-column label="操作类型"  align='center'>
              <template slot-scope="item">
                <p>{{item.row.status==1?'冻结金额':'取消冻结金额'}}</p>
              </template>
            </el-table-column>
            <el-table-column label="冻结金额" prop='money' align='center'/>
            <el-table-column label="操作时间" prop='create_time' align='center'/>
            <el-table-column label="备注" prop='remake' align='center'/>
          </el-table>
        </div>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button @click="hide" size='medium'>取 消</el-button>
        <el-button type="primary" @click="handleSubmit" size='medium'>确 定</el-button>
      </div>
    </el-dialog>
    <balanceDetails ref='balanceDetails'></balanceDetails>
  </div>
</template>

<script>
import formOperate from '@/layout/mixin/formOperate'
import balanceDetails from '../components/balanceDetails.vue'
import { export2Excel } from '@/utils/excel'
export default {
  name: 'Index',
  mixins: [formOperate],
  data() {
    return {
      url:'/user/admin/Balance',
      queryParam: {
        username: '',
        mobile:'',
        page:1,
        page_size:10
      },
      defaultQueryParam:['page','page_size'],
      formName:'formFrozen',
      formVisible2:false,
      formFieldsData:{
        frozen_balance:'',
        remake:'',
        balance:''
      },
      status:'', // 账户冻结 还是解冻
      details:'',
      frozenBalanceList:[]
    }
  },
  mounted() {

  },
  methods: {
    // 查看余额详情
    handleIncome(item) {
      this.$refs.balanceDetails.show(item.uid)
    },
    // 账户冻结
    handleFrozen(item,status) {
      this.formVisible2 = true
      this.status = status
      this.details = item
      if (this.status == 2) {
        this.frozenBalanceLog()
      }
    },
    // 全部冻结
    frozenAll() {
      this.formFieldsData.frozen_balance = this.details.balance
    },
    // 全部解冻
    balanceAll() {
      this.formFieldsData.balance = this.details.frozen_balance
    },
    //获取用户冻结解冻记录
    frozenBalanceLog() {
      this.$http.get('/user/admin/frozenBalanceLog?uid='+this.details.uid).then(response => {
        this.frozenBalanceList = response.data.data || response.data
      })
    },
    // 取消冻结
    hide() {
      this.formVisible2 = false
    },
    handleSubmit() {
      if (this.status == 1) {
        let param = {
          uid:this.details.uid,
          frozen_balance:this.formFieldsData.frozen_balance,
          remake:this.formFieldsData.remake
        }
        this.$http.post('/user/admin/frozenBalance', param).then(response => {
          if (response.code == 10000) {
            this.getList()
            this.hide()
          }
        })
      } else {
        let param = {
          uid:this.details.uid,
          balance:this.formFieldsData.balance,
          remake:this.formFieldsData.remake
        }
        this.$http.post('/user/admin/thawBalance', param).then(response => {
          if (response.code == 10000) {
            this.getList()
            this.hide()
          }
        })
      }
    },
    // 导出
    handleExport() {
      let columns = [
        {title:'用户名',key:'name'},
        {title:'账号',key:'mobile'},
        {title:'余额',key:'balance'},
        {title:'可提现',key:'withdrawal'},
        {title:'提现中',key:'withdrawal_in'},
        {title:'冻结中',key:'frozen_balance'},
        {title:'总收入',key:'count_income'},
        {title:'总支出',key:'count_pay'}
      ]
      let list = []
      this.data.forEach(res=>{
        list.push({
          name:res.user_info?(res.user_info.username || res.user_info.mobile):'',
          mobile:res.user_info?res.user_info.mobile:'',
          balance:res.balance,
          withdrawal:res.balance,
          withdrawal_in:res.withdrawal_in,
          frozen_balance:res.frozen_balance,
          count_income:res.count_income,
          count_pay:res.count_pay
        })
      })
      export2Excel(columns,list,'用户余额数据')
    },
  },
  components:{
    balanceDetails
  }
}
</script>
<style>
  .details .el-dialog__body{
    height: 700px;
    overflow-y: auto;
  }
</style>
<style lang="scss" scoped>

  .title{
    font-size: 16px;
    margin-bottom:10px;
  }
  .info{
    line-height: 30px;
  }
  .img_box{
    text-align: center;
  }
  .job_list{
    border:1px solid #eee;
    padding: 10px;
    border-radius: 4px;
  }
</style>